<?php
namespace app\techn_api_n\controller\v1;
use think\Db;
class Area extends Base
{
	public function _initialize()
	{
		parent::_initialize();
	}

	/**
	 * 获取二级联动地区
	 */
	public function get_area_two_level()
	{
		$list_data = cache('area_two_level_data');
		if(empty($list_data))
		{
			$list = Db::name('area')->field('id, id as value, pid, lev, name, name as label')->where(['lev' => ['ELT', 2], 'is_enable' => 1])->select();
			$list_data = array_tree_structure($list, 0, 'pid', 'children');
			cache('area_two_level_data',$list_data);
		}
// 		echo '<pre>';
// 		print_r($list_data);
// 		exit;
		if(!empty($list_data))
		{
			$result['code'] = 0;
			$result['msg'] = '请求成功';
			$result['data'] = $list_data;
		}else
		{
			$result['code'] = -1;
			$result['msg'] = '暂无数据';
		}
		echo json_encode($result);
	}

	/**
	 * 获取三级联动地区
	 */
	public function get_area_three_level()
	{
		$list_data = cache('area_three_level_data');
		if(empty($list_data))
		{
			$list = Db::name('area')->field('id, id as value, pid, lev, name, name as label')->where(['lev' => ['ELT', 3], 'is_enable' => 1])->select();
			$list_data = array_tree_structure($list, 0, 'pid', 'children');
			cache('area_three_level_data',$list_data);
		}
		if(!empty($list_data))
		{
			$result['code'] = 0;
			$result['msg'] = '请求成功';
			$result['data'] = $list_data;
		}else
		{
			$result['code'] = -1;
			$result['msg'] = '暂无数据';
		}
		echo json_encode($result);
	}
}